package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

import dao.ReplysDao;
import dao.TopicsDao;
import dao.UsersDao;
import dao.pojo.Topics;
import dao.pojo.Users;

public class TopicsDaoImpl extends BaseDaoImpl implements TopicsDao {
	private UsersDao usersDao;
	private ReplysDao replysDao;

	protected ReplysDao getReplysDao() {
		return replysDao;
	}

	public void setReplysDao(ReplysDao replysDao) {
		this.replysDao = replysDao;
	}

	public void addTopics(Topics topic) {
		Connection conn=getPool().getConnection();
		
		PreparedStatement pstmt=null;
		String sql="INSERT INTO files(title,user_id,submit_time,contents,hits)VALUES(?,?,?,?,?)";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, topic.getTitle());
			pstmt.setInt(2, topic.getUser().getId());
			pstmt.setTimestamp(3, new Timestamp(topic.getSubmitTime().getTime()));
			pstmt.setString(4, topic.getContent());
			pstmt.setInt(5, topic.getHits());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
	}

	public void deleteTopics(Topics topic) {
		Connection conn=getPool().getConnection();
		
		PreparedStatement pstmt=null;
		String sql="DELETE FROM topics WHERE id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, topic.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
		
	}

	public void deleteTopicsByUsers(Users user) {
		Connection conn=getPool().getConnection();
		
		PreparedStatement pstmt=null;
		String sql="DELETE FROM topics WHERE user_id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, user.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
	}

	public ArrayList<Topics> getAllTopics() {
		Connection conn=getPool().getConnection();
		
		ArrayList<Topics> topicsList=new ArrayList<Topics>();
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		String sql="SELECT * FROM topics";
		try {
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				Topics topic=new Topics();
				topic.setId(rs.getInt("id"));
				topic.setContent(rs.getString("contents"));
				topic.setHits(rs.getInt("hits"));
				topic.setSubmitTime(new Date(rs.getTimestamp("submit_time").getTime()));
				topic.setTitle(rs.getString("title"));
				Users user=new Users();
				user.setId(rs.getInt("user_id"));
				topic.setUser(usersDao.getUsersById(user));
				topicsList.add(topic);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
		return topicsList;
	}

	public Topics getTopics(Topics topic) {
		Connection conn=getPool().getConnection();
		
		Topics topics=null;
		
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		String sql="SELECT * FROM topics WHERE id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, topic.getId());
			rs=pstmt.executeQuery();
			while(rs.next()){
				topics=new Topics();
				topics.setId(rs.getInt("id"));
				topics.setContent(rs.getString("contents"));
				topics.setHits(rs.getInt("hits"));
				topics.setSubmitTime(new Date(rs.getTimestamp("submit_time").getTime()));
				topics.setTitle(rs.getString("title"));
				Users user=new Users();
				user.setId(rs.getInt("user_id"));
				topics.setUser(usersDao.getUsersById(user));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
		return topics;
	}

	public void saveTopics(Topics topic) {
		Connection conn=getPool().getConnection();

		PreparedStatement pstmt=null;
		String sql="UPDATE files SET title=?,user_id=?,submit_time=?,contents=?,hits=? WHERE id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, topic.getTitle());
			pstmt.setInt(2, topic.getUser().getId());
			pstmt.setTimestamp(3, new Timestamp(topic.getSubmitTime().getTime()));
			pstmt.setString(4, topic.getContent());
			pstmt.setInt(5, topic.getHits());
            pstmt.setInt(6,topic.getId());
            pstmt.executeUpdate();
        }catch(SQLException e){
             e.printStackTrace();
        }finally{
			getPool().freeConnection(conn);
		}
    }

	protected UsersDao getUsersDao() {
		return usersDao;
	}

	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}


}
